<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="goToDetail(goods)">
        <view class="goods-item">
          <!-- 商品左侧图片区域 -->
          <view class="goods-item-left">
            <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
          </view>
          <!-- 商品右侧信息区域 -->
          <view class="goods-item-right">
            <!-- 商品标题 -->
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="goods-info-box">
              <!-- 商品价格 -->
              <view class="goods-price">￥{{goods.goods_price.toFixed(2)}}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
    import {$http} from '@escook/request-miniprogram'
  export default {
    data() {
      return {
        // 请求参数对象
          queryObj: {
            query: '',
            cid: '',
            pagenum: 1,
            pagesize: 10,
          },
          goodsList: [],
          total: 0,
          
           defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
           isLoading: false
      };
    },

      
    onLoad(options) {
      
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      console.log(this.queryObj);
      
      this.getGoodsList(this.queryObj)
    },
    //
    methods: {
      async getGoodsList(prams, callback) {
        this.isLoading = true
        const res = await $http.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/search', prams )
        this.isLoading = false
        console.log(res);
        if (res.data.meta.status !== 200) {
          return uni.showToast({
            title:'请求失败',
            duration:1500,
            icon: 'none'
          })
          
        }
        // 存储数据
        this.goodsList = [...this.goodsList, ...res.data.message.goods]
        this.total = res.data.message.total
        console.log(this.goodsList,this.total);
        if (callback) {
          callback()
        }
      },
      
      goToDetail(goods) {
        uni.navigateTo({
          url: '/subPackge/goods/goods?goods_id=' + goods.goods_id
        })
      }
    },
    onReachBottom () {
      if (this.isLoading === true)  return 
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        return uni.showToast({
          title: '数据加载完毕...',
          duration:1500,
          icon: 'none'
        })
      }
      this.queryObj.pagenum += 1
      this.getGoodsList(this.queryObj)
    },
    onPullDownRefresh() {
      // 重置数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.isLoading = false
      // 重新发送请求
      this.getGoodsList(this.queryObj,uni.stopPullDownRefresh )
      
    }
  }
</script>

<style lang="scss">
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;

    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }

    .goods-price {
      font-size: 16px;
      color: #c00000;
    }
  }
}
</style>
